package myThread.xu.com;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class myThread extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        statusTv = (TextView)findViewById(R.id.Status);
        resultTv = (TextView)findViewById(R.id.Result);
        inputEt = (EditText)findViewById(R.id.Input);
        calculateBt = (Button)findViewById(R.id.Calculate);
        stopBt = (Button)findViewById(R.id.Stop);
        statusTv.setText("calc not started");
        resultTv.setText("0");
        calculateBt.setOnClickListener(new HandleStartButton(this));
        stopBt.setOnClickListener(new HandleStopButton(this));
    }
    
    private TextView statusTv;
    private TextView resultTv;
    private EditText inputEt;
    private Button calculateBt;
    private Button stopBt;
    private FactorialCalcThread th;
    
    private class HandleStopButton implements OnClickListener{

    	public Context context = null;
    	public HandleStopButton(Context c){
    		context = c;
    	}
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if ( th!=null ){
				th.done = true;
			} else
			{
				return;
			}

			try {
				th.join(); // wait for thread finish
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			th = null;
			hd.sendMessage(Message.obtain(hd, 2));

		}
    }
    private class HandleStartButton implements OnClickListener{
    	
    	public Context context = null;
    	public HandleStartButton(Context c){
    		context = c;
    	}
		@Override		
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			long input = 0;
			final String s = inputEt.getText().toString();
			try{
				input = (long)Integer.parseInt(s);
			} catch (NumberFormatException e){
				e.printStackTrace();
				Toast.makeText(context, "Input a valid data", Toast.LENGTH_SHORT).show();
				return;
			}
			if (th!=null){
				th.done = true;
			}
			th = new FactorialCalcThread(input);
			th.setPriority(Thread.MIN_PRIORITY);
			th.start();
		}
    }
    

    
	private Handler hd = new Handler() {
		@Override      
		public void handleMessage(Message msg) {
			switch(msg.what) {
			case 0:
				StringBuilder sb = (StringBuilder)msg.obj;
		        statusTv.setText("calculating");
		        resultTv.setText(sb.toString());
				break;
			case 1:
				StringBuilder sb1 = (StringBuilder)msg.obj;
		        statusTv.setText("calc finished");
		        resultTv.setText(sb1.toString());
		        break;
			case 2:
		        statusTv.setText("calc not started");
		        resultTv.setText("0");
		        break;
			default:
				break;
			}
		}
	};
	
    public class FactorialCalcThread extends Thread{
    	private long mFac;
    	private long mResult=0;
    	public boolean done = false;
    	public FactorialCalcThread ( long fac){
    		mFac = fac;
    	}

		@Override
		public void run() {
			// TODO Auto-generated method stub
		
			for ( long i =0; i<= mFac; i++) {
				if ( done == true){
					return;
				}
				mResult = mResult + i;
				StringBuilder sb = new StringBuilder();
				sb.append(mResult);
				hd.sendMessage(Message.obtain(hd, 0, sb));
			}
			
			StringBuilder sb = new StringBuilder();
			sb.append(mResult);
			hd.sendMessage(Message.obtain(hd, 1, sb));
		}
    }
}